package genedit.controller.interfaceActions;

import genedit.gui.GMainFrame;
import genedit.gui.GProjectTree;
import genedit.model.HierarchyElement;
import genedit.model.Package;
import genedit.model.Project;
import genedit.model.Workspace;
import genedit.model.graph.GraphDocument;
import genedit.model.graph.GraphElement;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;

/**
 * Akcija zadužena za kreiranje novog dijagrama.
 * 
 * @author Nikola
 * @version 1.0 5/11/13
 */
public class GNewDocumentA extends AbstractAction {
	private static final long serialVersionUID = 1L;

	protected static GNewDocumentA newD = null;

	protected GNewDocumentA() {
		putValue(NAME, "New Document");
		putValue(SHORT_DESCRIPTION, "New Document");
		putValue(SMALL_ICON, new ImageIcon("resources/icons/new.png"));
		putValue(MNEMONIC_KEY, KeyEvent.VK_N);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control B"));
	}

	public static AbstractAction getInstance() {
		if (newD == null)
			newD = new GNewDocumentA();
		return newD;
	}

	/**
	 * Akcija kreira novi dijagram unutar izabranog paketa i daje mu
	 * podrazumevani naziv.
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		Object p = GProjectTree.getInstance().getTree().getLastSelectedPathComponent();
		if (p instanceof Workspace || p instanceof Project || p instanceof GraphElement)
			return;

		Package pack = null;

		if (p instanceof Package) {
			pack = (Package) p;
		} else if (p instanceof GraphDocument) {
			pack = ((Package) ((GraphDocument) p).getParent());
		}

		GraphDocument gd = new GraphDocument((HierarchyElement) p, GMainFrame.getInstance()
				.getResourceBundle().getString("Diagram")
				+ " " + pack.getElementCount());
		pack.addElements(gd);

		GMainFrame.getInstance().addGraphDocumentView(gd);
	}
}